/**
 * @description: 路由跳转
 * @param {*} index 判断执行什么操作 1：返回 2：去普通页 3：去tab页面 4：跳转到指定页面，关闭其他页面
 * @param {*} url 目标跳转页面
 */
export function goPath(index, url) {
    if (index == 1) {
        // 返回上级页面
        uni.navigateBack({
            delta: url,
            animationType: "pop-out",
            animationDuration: 300,
        });
    } else if (index == 2) {
        // 页面跳转
        uni.navigateTo({
            url: url,
            animationType: "pop-in",
            animationDuration: 300,
        });
    } else if (index == 3) {
        // 跳转tab页面
        uni.switchTab({
            url: url,
            animationType: "pop-in",
            animationDuration: 300,
        });
    } else if (index == 4) {
        // 关闭所有页面跳转
        uni.reLaunch({
            url: url,
            animationType: "pop-in",
            animationDuration: 300,
        });
    } else if (index == 5) {
        uni.redirectTo({
            url: url,
            animationType: "pop-in",
            animationDuration: 300,
        });
    }
}
/**
 * 防抖函数
 * @param {*} fn 要执行的函数
 * @param {*} time 延迟执行的毫秒数 默认200毫秒
 */

export function debounce(fn, delay) {
    var delay = delay || 200;
    var timer = null;
    return function () {
        var args = arguments;
        var that = this;
        if (timer) {
            console.log("sss");
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            fn.apply(that, args);
        }, delay);
    };
}

/**
 * 显示轻消息
 * @param {*} title 显示的内容
 * @param {*} duration 显示时间，默认1.5s
 * @param {*} icon 显示图标，默认没有
 */

export function toast(title, duration = 2000, icon = "none") {
    uni.showToast({
        title,
        duration,
        icon,
    });
}

/**
 * 手机号校验
 * @param {*} mobile 手机号
 * @param {*} toast 校验不通过提示的消息内容
 * @returns
 */

export function verifyMobeil(mobile, toast = "请输入正确的手机号") {
    let regularMobile = /^1[3456789]\d{9}$/.test(mobile);
    if (mobile == "" || !regularMobile) {
        uni.showToast({
            title: toast,
            duration: 1500,
            icon: "none",
        });
        return;
    }
    return true;
}

/**
 * 密码校验
 * @param {*} password 密码(6-16位数字加字母)
 * @param {*} toast 校验不通过提示的消息内容
 * @returns
 */

export function verifyPassword(
    password,
    toast = "登录密码应为6-16位数字加字母"
) {
    let regularPassword = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(
        password
    );
    if (password == "" || !regularPassword) {
        uni.showToast({
            title: toast,
            duration: 1500,
            icon: "none",
        });
        return;
    }
    return true;
}
/**
 * 身份证校验
 * @param {*} sId
 * @returns
 */

export function isCardID(sId, toast = "你输入的身份证长度或格式错误") {
    if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
        uni.showToast({
            title: toast,
            duration: 1500,
            icon: "none",
        });
        return;
    }
    return true;
}

// 计算当前后一个月的日期
export function initDate() {
    let weekArrFont = ["日", "一", "二", "三", "四", "五", "六"];
    let oneDay = 24 * 60 * 60 * 1000;
    let monthDate = [];
    let date = new Date();
    let newDate = new Date().getTime();
    for (let i = 0; i < 7; i++) {
        let year = date.getFullYear();
        let dayTime = i * oneDay + newDate; // 每天对应的时间搓
        let itemDate = new Date(dayTime); // 每天的日期对象
        let font = weekArrFont[itemDate.getDay()];
        let dateMonth =
            itemDate.getMonth() + 1 < 10
                ? "0" + (itemDate.getMonth() + 1)
                : itemDate.getMonth() + 1;
        let dateDay =
            itemDate.getDate() < 10 ? "0" + itemDate.getDate() : itemDate.getDate();
        let day = `${year}-${dateMonth}-${dateDay}`;
        let name = `${dateDay}`;
        let item = {
            name,
            font,
            day,
        };
        monthDate.push(item);
    }
    return monthDate;
}
